﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace produccion.clases
{
    class CLSNOCPRD:ObservableObject
    {


        MDOgeneral mod = new MDOgeneral();

        public int insertar() {
            try {
                string query = "insert into NOCPRD (CODPRC_NCP,FECHA_NCP,CONSEC_NCP,ORDNRO_NCP,TALLA_NCP,CODDAN_NCP,CODPIE_NCP,EMPINF_NCP,EMPRES_NCP,CAUSA_NCP,QUEHACER_NCP,EMPDEC_NCP,NROPIE_NCP,OBSERV_NCP,CODREF_NCP,CODCOL_NCP) values ('" + CODPRC_NCP + "','" + FECHA_NCP.ToShortDateString() + "',"+CONSEC_NCP+",'" + ORDNRO_NCP + "','" + TALLA_NCP + "','" + CODDAN_NCP + "','" + CODPIE_NCP + "','" + EMPINF_NCP + "','" + EMPRES_NCP + "','" + CAUSA_NCP + "','" + QUEHACER_NCP + "','" + EMPDEC_NCP + "'," + NROPIE_NCP + ",'" + OBSERV_NCP + "','" + CODREF_NCP + "','" + CODCOL_NCP + "')";
                return mod.comando(query);
            }
            catch (Exception ex) { throw ex; }
        }

        public int actualizar() {
            try {
                string query = "update NOCPRD set ORDNRO_NCP='" + ORDNRO_NCP + "', TALLA_NCP='" + TALLA_NCP + "', CODDAN_NCP='" + CODDAN_NCP + "',CODPIE_NCP='" + CODPIE_NCP + "', EMPINF_NCP='" + EMPINF_NCP + "', EMPRES_NCP='" + EMPRES_NCP + "',CAUSA_NCP='" + CAUSA_NCP + "',QUEHACER_NCP='" + QUEHACER_NCP + "',EMPDEC_NCP='" + EMPDEC_NCP + "',NROPIE_NCP='" + NROPIE_NCP + "', OBSERV_NCP='" + OBSERV_NCP + "', CODREF_NCP='" + CODREF_NCP + "',CODCOL_NCP='" + CODCOL_NCP + "' where CODPRC_NCP='" + CODPRC_NCP + "' and FECHA_NCP='" + FECHA_NCP.ToShortDateString() + "'  and CONSEC_NCP=" + CONSEC_NCP;
                return mod.comando(query);
            }
            catch (Exception ex) { throw ex; }
        }

        public Boolean existe() {
            try {
                string query = "select count(*) from NOCPRD where CODPRC_NCP='" + CODPRC_NCP + "' and FECHA_NCP='" + FECHA_NCP.ToShortDateString() + "'  and CONSEC_NCP=" + CONSEC_NCP;
                DataTable val= mod.consulta(query);
                if (val.Rows.Count> 0) {
                    if (Convert.ToInt32(val.Rows[0][0])>0)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch (Exception ex){ throw ex;}        
        }

        public DataTable consDanFec() {
            try {
                string query = "select (select TRIM(nomsec_sec) from maesec where codsec_sec=CODPRC_NCP) as nomsec_sec,''||FECHA_NCP||'' as FECHA_NCP,CONSEC_NCP,ORDNRO_NCP,TALLA_NCP,(select TRIM(nomdan_dan) from tabdan where secdan_dan=CODPRC_NCP and coddan_dan=CODDAN_NCP) as nomdan_dan,(select TRIM(nombre_pie) from tabpie where codpie_pie=CODPIE_NCP) as  nombre_pie,EMPINF_NCP,EMPRES_NCP,(select TRIM(nombre_cau) from tabcau where codcau_cau=CAUSA_NCP) as nombre_cau,(select TRIM(nombre_sol) from tabsol where codsol_sol=QUEHACER_NCP) as nombre_sol,EMPDEC_NCP,NROPIE_NCP,OBSERV_NCP,CODREF_NCP,CODCOL_NCP from NOCPRD where FECHA_NCP='" + FECHA_NCP.ToShortDateString() + "'";
                return mod.consulta(query);
            }
            catch (Exception ex) { throw ex; }
        }


        public DataTable consDanOrd() {
            try {
                string query = "select talla_ncp,coddan_ncp, codpie_ncp, empinf_ncp, empres_ncp, causa_ncp, quehacer_ncp, empdec_ncp,nropie_ncp, observ_ncp, consec_ncp from nocprd where codprc_ncp='" + CODPRC_NCP + "' and ordnro_ncp='" + ORDNRO_NCP + "' order by talla_ncp,coddan_ncp";
                return mod.consulta(query);
            }
            catch (Exception ex) { throw ex; }
        }

        public Boolean guaMultiple() {
            try {

                MDOgeneral.transaccion = MDOgeneral.conexion.BeginTransaction();
                foreach (CLSNOCPRD r in lisNocPrd) {
                    r.consCon();
                    if (r.existe())
                    {
                        r.actualizar();
                    }
                    else {
                        r.insertar();
                    }
                }
                MDOgeneral.transaccion.Commit();
                return true;
            
            }
            catch (Exception ex) {
                MDOgeneral.transaccion.Rollback();
                throw ex; }

        }
        #region propieades

        public List<clases.CLSNOCPRD> lisNocPrd = new List<clases.CLSNOCPRD>();


        public int consCon() {
            try {
                string query = "select (max(CONSEC_NCP)+1) as CONSEC_NCP from NOCPRD";
                DataTable val = mod.consulta(query);
                if (val.Rows.Count > 0) {
                    CONSEC_NCP = Convert.ToInt32(val.Rows[0][0]);
                } else {
                    CONSEC_NCP = 0;
                }
                return CONSEC_NCP;
            }
            catch (Exception ex) { throw ex; }
        
        
        }

        string _CODPRC_NCP;
        public string CODPRC_NCP{
            get{return _CODPRC_NCP;}
            set{_CODPRC_NCP=value;
            RaisePropertyChangedEvent("CODPRC_NCP");
            }   
        }
        
        DateTime _FECHA_NCP;
        public DateTime FECHA_NCP{
            get{return _FECHA_NCP;}
            set{ _FECHA_NCP=value;
            RaisePropertyChangedEvent("FECHA_NCP");
            }
        }

        int _CONSEC_NCP;
        public int CONSEC_NCP{
        get{return _CONSEC_NCP;}
        set{_CONSEC_NCP=value;
        RaisePropertyChangedEvent("CONSEC_NCP");
        }
        }

        string _ORDNRO_NCP;
        public string ORDNRO_NCP{
        get{return _ORDNRO_NCP;}
        set{_ORDNRO_NCP=value;
        RaisePropertyChangedEvent("ORDNRO_NCP");
        }
        }

        string  _TALLA_NCP;
        public string TALLA_NCP{
        get{return _TALLA_NCP;}
        set{_TALLA_NCP=value;
        RaisePropertyChangedEvent("TALLA_NCP");
        }
        }

        string _CODDAN_NCP;        
        public string CODDAN_NCP{
        get{return _CODDAN_NCP;}
        set{_CODDAN_NCP=value;
        RaisePropertyChangedEvent("CODDAN_NCP");
        }
        }

        string _CODPIE_NCP;
        public string CODPIE_NCP{
        get{return _CODPIE_NCP;}
        set{_CODPIE_NCP=value;
        RaisePropertyChangedEvent("CODPIE_NCP");
        }
        }
            
        string _EMPINF_NCP;
        public string EMPINF_NCP{
        get{return _EMPINF_NCP;}
        set{_EMPINF_NCP=value;
        RaisePropertyChangedEvent("EMPINF_NCP");
        }
        }

        string _EMPRES_NCP;
        public string EMPRES_NCP{
        get{return _EMPRES_NCP;}
        set{_EMPRES_NCP=value;
        RaisePropertyChangedEvent("EMPRES_NCP");
        }
        }

        string _CAUSA_NCP;
        public string CAUSA_NCP{
        get{return _CAUSA_NCP;}
        set{_CAUSA_NCP=value;
        RaisePropertyChangedEvent("CAUSA_NCP");
        }
        }    
        
        string _QUEHACER_NCP;
        public string QUEHACER_NCP{
        get{return _QUEHACER_NCP;}
        set{_QUEHACER_NCP=value;
        RaisePropertyChangedEvent("QUEHACER_NCP");
        }
        }
 
        string _EMPDEC_NCP;
        public string EMPDEC_NCP{  
        get{return _EMPDEC_NCP;}
        set{_EMPDEC_NCP=value;
        RaisePropertyChangedEvent("EMPDEC_NCP");
        }
        }

        int _NROPIE_NCP;
        public int NROPIE_NCP{
        get{return _NROPIE_NCP;}
        set{_NROPIE_NCP=value;
        RaisePropertyChangedEvent("NROPIE_NCP");
        }
        }

        string _OBSERV_NCP;
        public string OBSERV_NCP{
        get{return _OBSERV_NCP;}
        set{_OBSERV_NCP=value;
        RaisePropertyChangedEvent("OBSERV_NCP");
        }
        }

        string _CODREF_NCP;
        public string CODREF_NCP{
        get{return _CODREF_NCP;}
        set{_CODREF_NCP=value;
        RaisePropertyChangedEvent("CODREF_NCP");
        }
        }
        
        string _CODCOL_NCP;
        public string CODCOL_NCP{
        get{return _CODCOL_NCP;}
        set{_CODCOL_NCP=value;
        RaisePropertyChangedEvent("CODCOL_NCP");
        }        
        }


        #endregion

    }
}
